package com.jk.smartelderlycare.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jk.smartelderlycare.dto.SysProcurement;
import com.jk.smartelderlycare.dto.SysPurchaseput;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.service.SysPurchaseputServierFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysPurchaseputController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/12/5 21:49
 */
@Api(tags = "采购退货管理")
@Slf4j
@RestController
@RequestMapping(value = "sysPurchaseput")
public class SysPurchaseputController {
    @Autowired
    private SysPurchaseputServierFeign sysPurchaseputServierFeign;
    @ApiOperation(value = "查询采购入库列表",httpMethod = "POST",notes = "分页查询")
    @PostMapping(value = "/findSysPurchaseputPaginationList")
    private ResponseResult<PagedResponse<SysPurchaseput>> findSysPurchaseputPaginationList(@RequestBody SysPurchaseput sysPurchaseput, @RequestParam(value = "pageNum")Integer pageNum , @RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200, "查询成功", sysPurchaseputServierFeign.findSysPurchaseputPaginationList(sysPurchaseput,pageNum,pageSize));
    }
    @ApiOperation(value = "删除",httpMethod = "DELETE",notes = "删除")
    @DeleteMapping(value = "deleteSysPurchaseputById")
    private ResponseResult deleteSysPurchaseputById(@RequestParam(value = "purchasePutid") Long purchasePutid){
        try {
            sysPurchaseputServierFeign.deleteSysPurchaseputById(purchasePutid);
            return new ResponseResult(200, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500, "删除失败");
        }
    }
    @ApiOperation(value = "审核",httpMethod = "PUT",notes = "审核")
    @PutMapping(value = "upStatusById")
    private ResponseResult upStatusById(@RequestParam(value = "purchasePutid") Long purchasePutid){
        try {
            sysPurchaseputServierFeign.upStatusById(purchasePutid);
            return new ResponseResult(200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500, "修改失败");
        }
    }
    @ApiOperation(value = "未审核",httpMethod = "PUT",notes = "未审核")
    @PutMapping(value = "downStatusById")
    private ResponseResult downStatusById(@RequestParam(value = "purchasePutid") Long purchasePutid){
        try {
            sysPurchaseputServierFeign.downStatusById(purchasePutid);
            return new ResponseResult(200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500, "修改失败");
        }
    }
    @ApiOperation(value = "关闭",httpMethod = "PUT",notes = "关闭")
    @PutMapping(value = "ShutdownStatusById")
    private ResponseResult ShutdownStatusById(@RequestParam(value = "purchasePutid") Long purchasePutid){
        try {
            sysPurchaseputServierFeign.ShutdownStatusById(purchasePutid);
            return new ResponseResult(200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500, "修改失败");
        }
    }

    /**
     * @description: 信息导出
     * @param: [video, response]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody SysPurchaseput sysPurchaseput, HttpServletResponse response){
        List<SysPurchaseput> PurchaseputList = sysPurchaseputServierFeign.findSysProcurement();

        //.xlsx格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setContentType("application/octet-stream; charset=UTF-8");


        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("信息表");
            //设置标题
            params.setTitle("信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, SysPurchaseput.class, PurchaseputList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    @ApiOperation(value = "角色信息导入", httpMethod = "POST", notes = "信息导入")
    @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(2);
            List<SysPurchaseput> list = ExcelImportUtil.importExcel(file.getInputStream(), SysPurchaseput.class, params);
            list.forEach(sysPurchaseput -> {

                sysPurchaseputServierFeign.addProcurement(sysPurchaseput);
            });
            return new ResponseResult(200, "导入用户信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入用户信息失败！");
        }
    }


}
